<template>
  <div class="detail-page">
    <van-nav-bar @click-left="$router.back()" fixed left-text="返回" title="面经详情" />
    <header class="header">
      <h1>{{ detailObj.stem }}</h1>
      <p>{{ detailObj.createdAt }} | {{detailObj.views }} 浏览量 | {{ detailObj.likeCount }} 点赞数</p>
      <p>
        <img :src="detailObj.avatar" alt />
        <span>{{ detailObj.creator }}</span>
      </p>
    </header>
    <main class="body">
      <!-- <p>我是内容</p>
      <p>我是内容</p>
      <p>我是内容</p>
      <p>我是内容</p>-->
      <div v-html="detailObj.content"></div>
    </main>
    <div class="opt">
      =={{detailObj.likeFlag }}
      <van-icon
        :class="{active:detailObj.likeFlag==1?true:false}"
        @click="linkHandle"
        name="like-o"
      />
      <van-icon name="star-o" />
    </div>
  </div>
</template>
<script>
import { DetailByIdAPI, LikeAPI } from '@/api/detail.js'
export default {
  name: '',
  data () {
    return {
      detailObj: {}
    }
  },
  created () {
    console.log(this.$route.params.id);
    this.getData()
  },
  computed: {
  },
  methods: {
    async getData () {
      const res = await DetailByIdAPI(this.$route.params.id)
      console.log(res, 8899);
      this.detailObj = res.data
    },
    // 点赞
    async linkHandle () {
      /*
      步骤分析：
      1、点击点赞图标
      2、发送请求，并且显示高亮
      3、再次点击高亮需要取消，
      4、显示点赞数量+1
      */
      await LikeAPI(
        {
          id: this.$route.params.id,
          optType: 1
        }
      )

      if (this.detailObj.likeFlag) {
        //表示已经点赞了===-1
        this.detailObj.likeCount--

      } else {
        //表示还没有点赞  +1
        this.detailObj.likeCount++

      }

      // 改变高亮
      this.detailObj.likeFlag = this.detailObj.likeFlag == 0 ? 1 : 0



    }
  }
}
</script>
<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #fec635;
        color: #fff;
      }
    }
  }
}
</style>
